package com.example.lock;

/**
 * @author DEO
 */


public class DeadLockDemo {

    // 共享锁对象
    private static final WandyLock lock1 = new WandyLock();
    private static final WandyLock lock2 = new WandyLock();

    static Thread t1 = new Thread(() -> {
        synchronized (lock1) {  // 持有lock1
            System.out.println("线程1已获取lock1");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程1已获取lock1，尝试获取lock2...");
            synchronized (lock2) {
                System.out.println("线程1已获取lock2，执行完成");
            }
        }
    }, "wandy-1");

    static Thread t2 = new Thread(() -> {
        synchronized (lock2) {  // 持有lock2
            System.out.println("线程2已获取lock2");
            try {

                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2已获取lock2，尝试获取lock1...");
            synchronized (lock1) {
                System.out.println("线程2已获取lock1，执行完成");
            }
        }
    }, "wandy-2");


    public static void main(String[] args) {
        t1.start();
        t2.start();
    }


    public static class WandyLock {

    }
}
